# 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。 
# 
#  请你将两个数相加，并以相同形式返回一个表示和的链表。 
# 
#  你可以假设除了数字 0 之外，这两个数都不会以 0 开头。 
# 
#  
# 
#  示例 1： 
# 
#  
# 输入：l1 = [2,4,3], l2 = [5,6,4]
# 输出：[7,0,8]
# 解释：342 + 465 = 807.
#  
# 
#  示例 2： 
# 
#  
# 输入：l1 = [0], l2 = [0]
# 输出：[0]
#  
# 
#  示例 3： 
# 
#  
# 输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
# 输出：[8,9,9,9,0,0,0,1]
#  
# 
#  
# 
#  提示： 
# 
#  
#  每个链表中的节点数在范围 [1, 100] 内 
#  0 <= Node.val <= 9 
#  题目数据保证列表表示的数字不含前导零 
#  
#  Related Topics 递归 链表 数学 
#  👍 6750 👎 0


from typing import List


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


# leetcode submit region begin(Prohibit modification and deletion)
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        head, tail = None, None
        carry = 0
        while l1 or l2:
            n1 = l1.val if l1 else 0
            n2 = l2.val if l2 else 0
            s = n1 + n2 + carry
            num = s % 10
            if head is None:
                head = tail = ListNode(num)
            else:
                tail.next = ListNode(num)
                tail = tail.next
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next
            carry = s // 10
        if carry > 0:
            tail.next = ListNode(carry)
        return head



# leetcode submit region end(Prohibit modification and deletion)


def log(*args, **kwargs):
    print(*args, **kwargs)


# 对应位置想加
# 对应位置想加 : n1 + n2 + 进位
# 位置值: (n1 + n2 + carry) mod 10
# 进位: (n1 + n2 + carry) / 10
# 长度不同, 认为短的前面是0
# 结束后还有进位, 附加节点, 值为进位
#     def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
#         head, tail = None, None
#         carry = 0
#         while l1 or l2:
#             n1 = l1.val if l1 else 0
#             n2 = l2.val if l2 else 0
#             s = n1 + n2 + carry
#             num = s % 10
#             carry = s // 10
#             if head is None:
#                 head = tail = ListNode(num)
#             else:
#                 tail.next = ListNode(num)
#                 tail = tail.next
#             if l1:
#                 l1 = l1.next
#             if l2:
#                 l2 = l2.next
#
#         if carry > 0:
#             tail.next = ListNode(carry)
#
#         return head



if __name__ == '__main__':
    s = Solution()
    # 输入：l1 = [2,4,3], l2 = [5,6,4]
    # 输出：[7,0,8]
    l1 = ListNode(2, ListNode(4, ListNode(3)))
    l2 = ListNode(5, ListNode(6, ListNode(4)))
    r = s.addTwoNumbers(l1, l2)
    # while r:
    #     log(r.val)
    #     r = r.next

    # 输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
    # 输出：[8,9,9,9,0,0,0,1]
    l3 = ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9)))))))
    l4 = ListNode(9, ListNode(9, ListNode(9, ListNode(9))))
    r2 = s.addTwoNumbers(l3, l4)
    while r2:
        log(r2.val)
        r2 = r2.next
